#include "FmodEngine.h"


void FV(FMOD_RESULT res)//fmod verification
{
	if(res != FMOD_OK)
	{
		assert("FMOD ERROR!");
		printf("FMOD error! (%d) %s\n", res, FMOD_ErrorString(res));
		exit(-1);
	}
}

void FmodEngine::Init()
{
	m_MasterVolume=1;
	m_MusicVolume=1;
	m_EffectVolume=1;
	m_pMusic=NULL;
	FV(System_Create(&m_pSystem));
	FV(m_pSystem->init(32,FMOD_INIT_NORMAL,0));
	FV(m_pSystem->createChannelGroup("effects",&m_EffectChannels));
}

void FmodEngine::Update()
{
	FV(m_pSystem->update());
}
void FmodEngine::Cleanup()
{
	FV(m_pSystem->release());
}

Sound* FmodEngine::LoadMusic(const char* file)
{
	if(m_pSystem->createStream(file,FMOD_LOOP_NORMAL|FMOD_2D|FMOD_SOFTWARE,0,&m_pMusic)!= FMOD_OK)
	{
		printf("Error loading music file\n");
	}
	return m_pMusic;
}

Sound* FmodEngine::LoadSample(const char* file, char* name)
{
	Sound* s;
	if(m_pSystem->createSound(file,FMOD_LOOP_NORMAL|FMOD_3D|FMOD_SOFTWARE,0,&s) == FMOD_OK)
	{
		m_pSamplesMap[name] = s;
	}
	else 
	{
		printf("Could not load sample: %s \n", file);
	}
	return s;
}

Channel* FmodEngine::PlayMusic()
{
	if(m_pMusic)
	{
		FV(m_pSystem->playSound(FMOD_CHANNEL_FREE,m_pMusic,true,&m_pMusicChannel));
		FV(m_pMusicChannel->setVolume(m_MasterVolume*m_MusicVolume));
		FV(m_pMusicChannel->setPaused(false));
		FV(m_pMusicChannel->setPriority(0));
	}
	else
	{
		printf("No music to play\n");
	}
	return m_pMusicChannel;
}

Channel* FmodEngine::PlayMusic(const char* file)
{
	LoadMusic(file);
	return PlayMusic();
}

Channel* FmodEngine::PlaySample(char* name)
{
	Channel* c;
	if(m_pSamplesMap.find(name)!=m_pSamplesMap.end())
	{
		
		m_pSystem->playSound(FMOD_CHANNEL_FREE,m_pSamplesMap[name],true,&c);
		c->setVolume(m_MasterVolume*m_EffectVolume);
		c->setPaused(false);
		c->setChannelGroup(m_EffectChannels);
	}
	else
	{
		printf("No sample of this name\n");
	}
	return c;
}

Channel* FmodEngine::PlaySample(const char* file, char* name)
{
	LoadSample(file,name);
	return PlaySample(name);
}

void FmodEngine::SetPauseMusic(bool b)
{
	m_pMusicChannel->setPaused(b);
}

void FmodEngine::SetPauseAll(bool b)
{
	m_pMusicChannel->setPaused(b);
	
	//not sure how to do this yet without storing all channels
}



//volume stuff
void FmodEngine::SetMasterVolume(float f)
{m_MasterVolume=f; 
if(m_MasterVolume>1){m_MasterVolume=1;}
if(m_MasterVolume<0){m_MasterVolume=0;}
m_pMusicChannel->setVolume(m_MasterVolume*m_MusicVolume);
m_EffectChannels->setVolume(m_MasterVolume*m_EffectVolume);
}
void FmodEngine::AdjustMasterVolume(float f)
{m_MasterVolume+=f; 
if(m_MasterVolume>1){m_MasterVolume=1;}
if(m_MasterVolume<0){m_MasterVolume=0;}
m_pMusicChannel->setVolume(m_MasterVolume*m_MusicVolume);
m_EffectChannels->setVolume(m_MasterVolume*m_EffectVolume);
}

void FmodEngine::SetMusicVolume(float f)
{m_MusicVolume=f; 
if(m_MusicVolume>1){m_MusicVolume=1;}
if(m_MusicVolume<0){m_MusicVolume=0;}
m_pMusicChannel->setVolume(m_MasterVolume*m_MusicVolume);
}
void FmodEngine::AdjustMusicVolume(float f)
{m_MusicVolume+=f; 
if(m_MusicVolume>1){m_MusicVolume=1;}
if(m_MusicVolume<0){m_MusicVolume=0;}
m_pMusicChannel->setVolume(m_MasterVolume*m_MusicVolume);
}

void FmodEngine::SetEffectVolume(float f)
{m_EffectVolume=f; 
if(m_EffectVolume>1){m_EffectVolume=1;}
if(m_EffectVolume<0){m_EffectVolume=0;}
m_EffectChannels->setVolume(m_MasterVolume*m_EffectVolume);
}
void FmodEngine::AdjustEffectVolume(float f)
{m_EffectVolume+=f; 
if(m_EffectVolume>1){m_EffectVolume=1;}
if(m_EffectVolume<0){m_EffectVolume=0;}
m_EffectChannels->setVolume(m_MasterVolume*m_EffectVolume);
}

Sound* FmodEngine::LoadMusic(const char* file, Sound* m)
{
	if(m_pSystem->createStream(file,FMOD_LOOP_NORMAL|FMOD_2D|FMOD_SOFTWARE,0,&m)!= FMOD_OK)
	{
		printf("Error loading music file\n");
	}
	return m;
}
Channel* FmodEngine::PlayMusic(Sound* m, Channel* c)
{
	if(m)
	{
		FV(m_pSystem->playSound(FMOD_CHANNEL_FREE,m,true,&c));
		FV(c->setPriority(0));
	}
	else
	{
		printf("No music to play\n");
	}
	return c;
}

Sound* FmodEngine::LoadSample(const char* file,char* name, Sound* s)
{
	if(m_pSystem->createSound(file,FMOD_LOOP_NORMAL|FMOD_3D|FMOD_SOFTWARE,0,&s) == FMOD_OK)
	{
		m_pSamplesMap[name] = s;
	}
	else 
	{
		printf("Could not load sample: %s \n", file);
	}
	return s;
}
Channel* FmodEngine::PlaySample(Sound* s,Channel* c)
{
	if(s)
	{
		m_pSystem->playSound(FMOD_CHANNEL_FREE,s,true,&c);
		c->setChannelGroup(m_EffectChannels);
	}
	else
	{
		printf("No sample of this name\n");
	}
	return c;
}